package evi.com.evi.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import evi.com.evi.R;
import evi.com.evi.bean.DisposeOrderBean;
import evi.com.evi.bean.SubOrderDetailBean;
import evi.com.evi.utils.OrderStatus;
import evi.com.evi.view.MyGridView;

/**
 * Created by sunyu on 2017/3/11.
 */

public class DealOrderAdapter extends RecyclerView.Adapter<DealOrderAdapter.MyViewHolder> implements Filterable {

    private Context context;
    private List<DisposeOrderBean> list;
    //用来存储过滤的结果集
    private List<DisposeOrderBean> copyList;

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_order_time;        //时间
        TextView tv_text_1;            //首三项
        TextView tv_text_2;
        TextView tv_text_2_2;           //状态
        TextView tv_text_3;

        TextView tv_user;                //收货人
        TextView tv_phone;                //收货人号码
        TextView tv_trans_type;        //配送方式
        TextView tv_peisong_user;        //配送员
        TextView tv_peisong_user_phone; //配送员电话
        TextView tv_cancle_reason;    //取消订单原因
        TextView tv_refuse_reason;    //拒绝原因
        TextView tv_money;                //订单金额
        TextView tv_detail;            //隐藏详情
        TextView tv_refuse;            //拒绝
        TextView tv_btn_manager;       //按钮

        FrameLayout fl_goods;            //商品表
        MyGridView gv_goods;      //商品表

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_order_time = (TextView) itemView.findViewById(R.id.tv_order_time);
            tv_text_1 = (TextView) itemView.findViewById(R.id.tv_text_1);
            tv_text_2 = (TextView) itemView.findViewById(R.id.tv_text_2);
            tv_text_2_2 = (TextView) itemView.findViewById(R.id.tv_text_2_2);
            tv_text_3 = (TextView) itemView.findViewById(R.id.tv_text_3);
            tv_user = (TextView) itemView.findViewById(R.id.tv_user);
            tv_phone = (TextView) itemView.findViewById(R.id.tv_phone);
            tv_trans_type = (TextView) itemView.findViewById(R.id.tv_trans_type);
            tv_peisong_user = (TextView) itemView.findViewById(R.id.tv_peisong_user);
            tv_cancle_reason = (TextView) itemView.findViewById(R.id.tv_cancle_reason);
            tv_refuse_reason = (TextView) itemView.findViewById(R.id.tv_refuse_reason);
            tv_money = (TextView) itemView.findViewById(R.id.tv_money);
            tv_detail = (TextView) itemView.findViewById(R.id.tv_detail);
            tv_refuse = (TextView) itemView.findViewById(R.id.tv_refuse);
            tv_btn_manager = (TextView) itemView.findViewById(R.id.tv_btn_manager);
            tv_peisong_user_phone = (TextView) itemView.findViewById(R.id.tv_peisong_user_phone);
            fl_goods = (FrameLayout) itemView.findViewById(R.id.fl_goods);
            gv_goods = (MyGridView) itemView.findViewById(R.id.gv_goods);
        }
    }

    public DealOrderAdapter(Context context, List<DisposeOrderBean> list) {
        this.context = context;
        this.list = list;
        this.copyList = new ArrayList();
        this.copyList.addAll(list);
    }

    public void clearResult() {
        if (list != null) list.clear();
        if (copyList != null) copyList.clear();
    }

    public void addResult(DisposeOrderBean bean) {
        if (list == null) {
            list = new ArrayList<DisposeOrderBean>();
        }
        if (copyList == null) {
            copyList = new ArrayList<DisposeOrderBean>();
        }
        list.add(bean);
        copyList.add(bean);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_lv_deal_order_dispose, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final DisposeOrderBean bean = list.get(position);
        String state = bean.getState().toString();
        final Integer orderStatus = bean.getOrderStatus();
        final Integer orderCancelStatus = bean.getOrderCancelStatus();
        String type = bean.getType().toString();
        final Integer deliverType = bean.getDeliverType();
        //显示相关内容
        holder.tv_text_1.setText(bean.getOrder());      //订单
        holder.tv_text_3.setText(bean.getTime());       //出发时间

        holder.tv_text_2_2.setText(bean.getState());      //状态

        holder.tv_order_time.setText(bean.getOrder_time());
        holder.tv_user.setText(bean.getName());         //收货人
        holder.tv_phone.setText(bean.getTelnum());      //手机号码
        holder.tv_trans_type.setText(bean.getType());   //配送方式
        holder.tv_peisong_user.setText(bean.getServiceProviderName());//配送员
        holder.tv_cancle_reason.setText(bean.getReason()); //取消原因
        holder.tv_refuse_reason.setText(bean.getRefuse()); //拒绝原因
        holder.tv_peisong_user_phone.setText(bean.getServiceProviderPhoneNo());
        holder.tv_money.setText(bean.getMoney());
        //
        holder.tv_detail.setText("展开详情");

        //设置子订单商品
        List<SubOrderDetailBean> foodList = bean.getFoodList();
        GridGoodsAdapter goodsAdapter = new GridGoodsAdapter(context, foodList);
        holder.gv_goods.setAdapter(goodsAdapter);

        //初始化不显示如下信息
        holder.tv_text_2.setVisibility(View.GONE);
        holder.tv_user.setVisibility(View.GONE);
        holder.tv_phone.setVisibility(View.GONE);
        holder.tv_trans_type.setVisibility(View.GONE);
        holder.tv_peisong_user.setVisibility(View.GONE);
        holder.tv_cancle_reason.setVisibility(View.GONE);
        holder.tv_refuse_reason.setVisibility(View.GONE);
        holder.tv_peisong_user_phone.setVisibility(View.GONE);
        //holder.gv_goods.setVisibility(View.GONE);

        holder.tv_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone(bean.getTelnum());
            }
        });

        Log.e("TAG", "orderStatus: " + orderStatus);

        //待取货 服务人员已接单  服务人员已取货 已完成
        if (orderStatus != null &&
                (orderStatus == OrderStatus.MAINORDER_STATUS_ACCEPT
                        || orderStatus == OrderStatus.MAINORDER_STATUS_SERVICER_ACCEPT
                        || orderStatus == OrderStatus.MAINORDER_STATUS_COMPLETED
                        || orderStatus == OrderStatus.MAINORDER_STATUS_SERVICER_GET_GOOD
                        || orderStatus == OrderStatus.MAINORDER_STATUS_USER_GET_GOOD)) {
            //配送员送
            if (deliverType != null && OrderStatus.DELIVER_TYPE_SEND == deliverType) {
                //绑定事件
                holder.tv_detail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                        if (holder.tv_detail.getText().toString().trim().equals("展开详情")) {
                            holder.tv_detail.setText("隐藏详情");
                            //holder.gv_goods.setVisibility(View.VISIBLE);
                            holder.tv_user.setVisibility(View.VISIBLE);
                            holder.tv_phone.setVisibility(View.VISIBLE);
                            holder.tv_peisong_user.setVisibility(View.VISIBLE);
                            holder.tv_trans_type.setVisibility(View.VISIBLE);
                            if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_APPEAL) {
                                holder.tv_cancle_reason.setVisibility(View.VISIBLE);
                            }
                            if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_REFUSED) {
                                holder.tv_refuse_reason.setVisibility(View.VISIBLE);
                            }
                        } else {
                            holder.tv_detail.setText("展开详情");
                            //holder.gv_goods.setVisibility(View.GONE);
                            holder.tv_user.setVisibility(View.GONE);
                            holder.tv_phone.setVisibility(View.GONE);
                            holder.tv_peisong_user.setVisibility(View.GONE);
                            holder.tv_trans_type.setVisibility(View.GONE);
                            if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_APPEAL) {
                                holder.tv_cancle_reason.setVisibility(View.GONE);
                            }
                            if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_REFUSED) {
                                holder.tv_refuse_reason.setVisibility(View.GONE);
                            }
                        }
                    }
                });
            } else {
                //绑定事件
                holder.tv_detail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                        if (holder.tv_detail.getText().toString().equals("展开详情")) {
                            holder.tv_detail.setText("隐藏详情");
                            //holder.gv_goods.setVisibility(View.VISIBLE);
                            holder.tv_user.setVisibility(View.VISIBLE);
                            holder.tv_phone.setVisibility(View.VISIBLE);
                            holder.tv_trans_type.setVisibility(View.VISIBLE);
                            if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_APPEAL) {
                                holder.tv_cancle_reason.setVisibility(View.VISIBLE);
                            }
                            if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_REFUSED) {
                                holder.tv_refuse_reason.setVisibility(View.VISIBLE);
                            }
                        } else {
                            holder.tv_detail.setText("展开详情");
                            //.gv_goods.setVisibility(View.GONE);
                            holder.tv_user.setVisibility(View.GONE);
                            holder.tv_phone.setVisibility(View.GONE);
                            holder.tv_trans_type.setVisibility(View.GONE);
                            if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_APPEAL) {
                                holder.tv_cancle_reason.setVisibility(View.GONE);
                            }
                            if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_REFUSED) {
                                holder.tv_refuse_reason.setVisibility(View.GONE);
                            }
                        }
                    }
                });
            }
        } else if (orderStatus != null && orderStatus == OrderStatus.MAINORDER_STATUS_REJECT) {
            //商家拒绝接单
            if (deliverType != null && OrderStatus.DELIVER_TYPE_SEND == deliverType) {
                holder.tv_detail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (holder.tv_detail.getText().toString().equals("展开详情")) {
                            holder.tv_detail.setText("隐藏详情");
                            //holder.gv_goods.setVisibility(View.VISIBLE);
                            holder.tv_user.setVisibility(View.VISIBLE);
                            holder.tv_phone.setVisibility(View.VISIBLE);
                            holder.tv_peisong_user.setVisibility(View.VISIBLE);
                            holder.tv_trans_type.setVisibility(View.VISIBLE);
                            holder.tv_refuse_reason.setVisibility(View.VISIBLE);

                        } else {
                            holder.tv_detail.setText("展开详情");
                            //holder.gv_goods.setVisibility(View.GONE);
                            holder.tv_user.setVisibility(View.GONE);
                            holder.tv_phone.setVisibility(View.GONE);
                            holder.tv_peisong_user.setVisibility(View.GONE);
                            holder.tv_trans_type.setVisibility(View.GONE);
                            holder.tv_refuse_reason.setVisibility(View.GONE);

                        }
                    }
                });
            } else {
                holder.tv_detail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (holder.tv_detail.getText().toString().equals("展开详情")) {
                            holder.tv_detail.setText("隐藏详情");
                            //holder.gv_goods.setVisibility(View.VISIBLE);
                            holder.tv_user.setVisibility(View.VISIBLE);
                            holder.tv_phone.setVisibility(View.VISIBLE);
                            holder.tv_trans_type.setVisibility(View.VISIBLE);
                            holder.tv_refuse_reason.setVisibility(View.VISIBLE);
                        } else {
                            holder.tv_detail.setText("展开详情");
                            //holder.gv_goods.setVisibility(View.GONE);
                            holder.tv_user.setVisibility(View.GONE);
                            holder.tv_phone.setVisibility(View.GONE);
                            holder.tv_trans_type.setVisibility(View.GONE);
                            holder.tv_refuse_reason.setVisibility(View.GONE);
                        }
                    }
                });
            }

        } else if (orderStatus != null && orderStatus == OrderStatus.MAINORDER_STATUS_CANCELED) {
            //定单已取消
            //配送员送
            if (deliverType != null && OrderStatus.DELIVER_TYPE_SEND == deliverType) {
                holder.tv_detail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (holder.tv_detail.getText().toString().equals("展开详情")) {
                            holder.tv_detail.setText("隐藏详情");
                            //holder.gv_goods.setVisibility(View.VISIBLE);
                            holder.tv_user.setVisibility(View.VISIBLE);
                            holder.tv_phone.setVisibility(View.VISIBLE);
                            holder.tv_peisong_user.setVisibility(View.VISIBLE);
                            holder.tv_trans_type.setVisibility(View.VISIBLE);
                            holder.tv_cancle_reason.setVisibility(View.VISIBLE);

                        } else {
                            holder.tv_detail.setText("展开详情");
                            //holder.gv_goods.setVisibility(View.GONE);
                            holder.tv_user.setVisibility(View.GONE);
                            holder.tv_phone.setVisibility(View.GONE);
                            holder.tv_peisong_user.setVisibility(View.GONE);
                            holder.tv_trans_type.setVisibility(View.GONE);
                            holder.tv_cancle_reason.setVisibility(View.GONE);
                        }
                    }
                });
            } else {
                holder.tv_detail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (holder.tv_detail.getText().toString().equals("展开详情")) {
                            holder.tv_detail.setText("隐藏详情");
                            //holder.gv_goods.setVisibility(View.VISIBLE);
                            holder.tv_user.setVisibility(View.VISIBLE);
                            holder.tv_phone.setVisibility(View.VISIBLE);
                            holder.tv_trans_type.setVisibility(View.VISIBLE);
                            holder.tv_cancle_reason.setVisibility(View.VISIBLE);
                        } else {
                            holder.tv_detail.setText("展开详情");
                            //holder.gv_goods.setVisibility(View.GONE);
                            holder.tv_user.setVisibility(View.GONE);
                            holder.tv_phone.setVisibility(View.GONE);
                            holder.tv_trans_type.setVisibility(View.GONE);
                            holder.tv_cancle_reason.setVisibility(View.GONE);
                        }
                    }
                });
            }
        } else {
            holder.tv_detail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    if (holder.tv_detail.getText().toString().equals("展开详情")) {
                        holder.tv_detail.setText("隐藏详情");
                        //holder.gv_goods.setVisibility(View.VISIBLE);
                        holder.tv_user.setVisibility(View.VISIBLE);
                        holder.tv_phone.setVisibility(View.VISIBLE);
                        holder.tv_trans_type.setVisibility(View.VISIBLE);
                        if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_APPEAL) {
                            holder.tv_cancle_reason.setVisibility(View.VISIBLE);
                        }
                        if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_REFUSED) {
                            holder.tv_refuse_reason.setVisibility(View.VISIBLE);
                        }
                    } else {
                        holder.tv_detail.setText("展开详情");
                        //.gv_goods.setVisibility(View.GONE);
                        holder.tv_user.setVisibility(View.GONE);
                        holder.tv_phone.setVisibility(View.GONE);
                        holder.tv_trans_type.setVisibility(View.GONE);
                        if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_APPEAL) {
                            holder.tv_cancle_reason.setVisibility(View.GONE);
                        }
                        if (orderCancelStatus != null && orderCancelStatus == OrderStatus.MAINORDER_CANCEL_STATUS_REFUSED) {
                            holder.tv_refuse_reason.setVisibility(View.GONE);
                        }
                    }
                }
            });
        }

    }

    /**
     * 拨打电话
     *
     * @param phone 电话号
     */

    public void phone(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    @Override
    public Filter getFilter() {
        return new DealOrderFilter(this);
    }

    /**
     * 过滤类，根据输入的电话或者订单号过滤
     */
    private static class DealOrderFilter extends Filter {

        private final DealOrderAdapter adapter;

        private DealOrderFilter(DealOrderAdapter adapter) {
            super();
            this.adapter = adapter;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
//            filteredList.clear();
            //初始化过滤结果对象
            final FilterResults results = new FilterResults();
            //假如搜索为空的时候，将复制的数据添加到原始数据，用于继续过滤操作
            if (results.values == null) {
                adapter.list.clear();
                adapter.list.addAll(adapter.copyList);
            }
            //关键字为空的时候，搜索结果为复制的结果
            if (constraint.length() == 0) {
                results.values = adapter.copyList;
                results.count = adapter.copyList.size();
            } else {
//                final String filterPattern = constraint.toString().toLowerCase().trim();

                final List<DisposeOrderBean> newValues = new ArrayList<DisposeOrderBean>();

                for (final DisposeOrderBean user : this.adapter.copyList) {
                    if (user.getTelnum() != null && user.getTelnum().contains(constraint)) {
                        newValues.add(user);
                    } else if (user.getOrder() != null && user.getOrder().contains(constraint)) {
                        newValues.add(user);
                    }
                }
                results.values = newValues;
                results.count = newValues.size();
                Log.e("EE", "===new Value=" + newValues.size());

            }

            Log.e("EE", "--执行performFilter.constraint=" + constraint + ",copyJiedanList.size=" + this.adapter.copyList.size() + ",result=" + (((ArrayList<DisposeOrderBean>) results.values).size()));
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            Log.e("EE", "--publishResults.constraint=" + constraint + ",originalList.size=" + this.adapter.copyList.size() + ",result=" + (((ArrayList<DisposeOrderBean>) results.values).size()));
            adapter.list.clear();//清除原始数据
            adapter.list.addAll((ArrayList<DisposeOrderBean>) results.values);//将过滤结果添加到这个对象

//            adapter.pendingOrderPager.disposeFalseFragment.getCount(adapter.pendingOrderPager,"" +results.count);

            if (results.count > 0) {

                adapter.notifyDataSetChanged();//有关键字的时候刷新数据
            } else {
                //关键字不为零但是过滤结果为空刷新数据
                if (constraint.length() != 0) {
                    adapter.notifyDataSetChanged();
                    return;
                }
                //加载复制的数据，即为最初的数据
                adapter.list.addAll(adapter.copyList);
            }
        }
    }
}
